<template>
  <div class="xtx-checkbox" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-checked"></i>
    <i v-else class="iconfont icon-unchecked"></i>
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>
<script lang="ts">
export default {
  name: 'XtxCheckbox'
}
</script>
<script setup lang="ts">
import { toRef } from 'vue'
import { useVModel } from '@vueuse/core'

// 接收参数modelValue,和函数emit
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false
    }
})
// 实现双向绑定
// const modelValue = toRef(props,'modelValue')
// const changeChecked = () => {
//     emit('update:modelValue',!modelValue.value)
// }

// 使用@vueuse/core实现双向绑定
const checked = useVModel(props, 'modelValue', emit)
const changeChecked = () => {
    checked.value = !checked.value
}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  cursor: pointer;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>